// pages/music-player/index.js
import { audioContext, playerStore } from "../../store/index"
import parseLyric from '../../utils/parse-lyric'
const playModeNames = ["order", "repeat", "random"]
Page({
  data: {
    id: "",    // 歌曲id
    songInfo: {},  // 歌曲信息
    songLyric: [],  // 歌词信息
    durationTime: '',    // 歌曲总时长
    currentTime: '',  // 记录当前播放时间
    currentIndex: 0,  // 记录当前歌词位置
    currentLyricText: "",  // 当前歌词
    lyricScrollTop: 0,    // 歌词滚动距离
    currentPage: 0,  // 切换页面
    contentHeight: "",  // 主题内容高度
    isSongLyric: true,   // 是否展示歌词
    playModeIdex: 0,   // 播放模式
    playModeName: "order",  // 播放模式名称
    playListSongs: [],   // 播放的歌单
    playListIndex: 0,    // 歌曲所在歌单的位置
    isPlaying: true,   // 是否播放
    isShowPlaylist: false,  // 显示播放列表
  },
  onLoad(options) {
    const id = options.id
    this.setData({ id })
    this.setupPlayerStoreListenter()
    // 动态计算内容大小
    const globalData = getApp().globalData
    const contentHeight = globalData.screenHeight - globalData.statusBarHeight - 44
    const deviceRadio = globalData.deviceRadio
    this.setData({ contentHeight, isSongLyric: deviceRadio >= 2 })
  },
  // 监听页面切换
  handleSwiperChange(event) {
    this.setData({ currentPage: event.detail.current })
  },
  // 监听滑块拖动完成事件
  handleSliderChange(event) {
    const value = event.detail.value
    // 先暂停 再跳转到指定位置
    // audioContext.pause()
    audioContext.seek(value / 1000)  // 单位：秒
    this.setData({ currentTime: value })
  },
  // 监听滑块拖动过程事件
  handleSliderChangeing(event) {
    const value = event.detail.value
    audioContext.pause()
    this.setData({ currentTime: value })
  },
  /////////////// 按钮事件 ///////////////
  // 改变播放模式
  handleModeBtnClick() {
    let playModeIdex = this.data.playModeIdex + 1
    if (playModeIdex === playModeNames.length) {
      playModeIdex = 0
    }
    playerStore.setState('playModeIdex', playModeIdex)
  },
  // 暂停/开始音乐
  handlePlayBtnClick() {
    playerStore.dispatch("changeMusicIsPlayingAction")
  },
  // 上一首
  handlePrevSongClick() {
    playerStore.dispatch("changeNewMusicAction", "prev")
  },
  // 下一首
  handleNextSongClick() {
    playerStore.dispatch("changeNewMusicAction", "next")
  },
  // 显示播放列表
  handleShowPlaylistClick() {
    this.setData({ isShowPlaylist: !this.data.isShowPlaylist })
  },
  // 点击播放列表歌曲跳转
  handleJumpClick(event){
    this.setData({isShowPlaylist:false})
    const playListIndex = event.currentTarget.dataset.item[1]
    const id = event.currentTarget.dataset.item[0].id
    playerStore.dispatch("playMusicIdInfoAction",{id,isRefresh:true})
    playerStore.setState("playListIndex",playListIndex)
  },

  setupPlayerStoreListenter() {
    // 监听音乐播放状态
    playerStore.onStates(["songInfo", "songLyric", "durationTime", "currentTime", "currentIndex", "currentLyricText"], ({
      songInfo, songLyric, durationTime, currentTime, currentIndex, currentLyricText,
    }) => {
      if (songInfo) { this.setData({ songInfo }) }
      if (songLyric) { this.setData({ songLyric }) }
      if (durationTime) { this.setData({ durationTime }) }
      if (currentTime) { this.setData({ currentTime }) }
      if (currentIndex) { this.setData({ currentIndex, lyricScrollTop: currentIndex * 35 }) }
      if (currentLyricText) { this.setData({ currentLyricText }) }
    })
    // 监听播放模式相关状态
    playerStore.onStates(["playModeIdex", "isPlaying"], ({ playModeIdex, isPlaying }) => {
      if (playModeIdex != undefined) {
        this.setData({ playModeIdex, playModeName: playModeNames[playModeIdex] })
      }
      if (isPlaying != undefined) {
        this.setData({ isPlaying: isPlaying })
      }
    })
    // 监听歌曲所在的歌单状态
    playerStore.onStates(["playListSongs", "playListIndex"], ({ playListSongs, playListIndex }) => {
      if(playListSongs){
        this.setData({playListSongs})
      }
      if(playListIndex != undefined){
        this.setData({playListIndex})
      }
    })
  },
  onUnload() {

  },
})